using System;
using System.Collections.Generic;
using System.Text;

using NBusiness.ESharp;
using NBusiness.CodeDom;
using NBusiness;

using NBusiness.CodeDom.Compiler;
using NBusiness.ESharp.Compiler;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBusiness.ESharp.Testing.Properties;
using NBusiness.ESharp.Testing.Mocks;
using NBusiness.CodeDom.Services;

namespace NBusiness.ESharp.Testing
{
    [TestClass]
    public class ValidationRuleTests
    {
        [TestMethod]
        public void RuleAbbreviationTestSuccess1()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Rules.Abbreviation1.es", Resources.Rules_Abbreviation1 ));

            Assert.AreEqual(0, errorService.Errors.Count);

            EntityRule actual = root.Families["Rules"].Entities[0].Rules[0];
            Assert.IsInstanceOfType(actual, typeof(EntityValidation));
            Assert.AreSame(root.Families["Rules"].Entities[0].Fields[1], (actual as EntityValidation).Validatable);
}

        [TestMethod]
        public void SimpleTestSuccess1()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Rules.Simple1.es", Resources.Rules_Simple1 ));

            Assert.AreEqual(0, errorService.Errors.Count);

            EntityRule actual = root.Families["Rules"].Entities[0].Rules[0];
            Assert.IsInstanceOfType(actual, typeof(EntityValidation));
            Assert.AreSame(root.Families["Rules"].Entities[0].Fields[1], (actual as EntityValidation).Validatable);
        }

        [TestMethod]
        public void SimpleTestSuccess2()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Rules.Simple2.es", Resources.Rules_Simple2 ));

            Assert.AreEqual(0, errorService.Errors.Count);

            EntityRule actual = root.Families["Rules"].Entities[0].Rules[0];
            Assert.IsInstanceOfType(actual, typeof(EntityValidation));
            Assert.AreSame(root.Families["Rules"].Entities[0].Fields[1], (actual as EntityValidation).Validatable);
        }

        [TestMethod]
        public void SimpleTestSuccess3()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Rules.Simple3.es", Resources.Rules_Simple3 ));

            Assert.AreEqual(0, errorService.Errors.Count);

            EntityRule actual = root.Families["Rules"].Entities[0].Rules[0];
            Assert.IsInstanceOfType(actual, typeof(EntityValidation));
            Assert.AreSame(root.Families["Rules"].Entities[0].Fields[1], (actual as EntityValidation).Validatable);

            EntityValidation rule = root.Families["Rules"].Entities[0].Rules[0] as EntityValidation;
            Assert.AreEqual(2, rule.Arguments.Count);
            Assert.AreEqual("a", rule.Arguments[0].Name);
            Assert.AreEqual("b", rule.Arguments[1].Name);
            Assert.AreEqual("1.0", rule.Arguments[0].Value);
            Assert.AreEqual("1/1/1980", rule.Arguments[1].Value);

        }

        [TestMethod]
        public void MissingUsingTest()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Rules.MissingUsing.es", Resources.Rules_MissingUsing ));

            Assert.AreEqual(1, errorService.Errors.Count);
            Assert.AreEqual(ESharpErrorCode.ES5002.ToString(), errorService.Errors[0].ErrorCode);
            Assert.AreEqual(EntityBuildErrorType.Error, errorService.Errors[0].Type);
        }

        [TestMethod]
        public void SimpleTestFail1()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Rules.Fail1.es", Resources.Rules_Fail1 ));

            Assert.AreEqual(1, errorService.Errors.Count);
            Assert.AreEqual(ESharpErrorCode.ES5002.ToString(), errorService.Errors[0].ErrorCode);
            Assert.AreEqual(EntityBuildErrorType.Error, errorService.Errors[0].Type);
        }

        [TestMethod]
        public void SimpleTestFail2()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Rules.Fail2.es", Resources.Rules_Fail2 ));

            Assert.AreEqual(1, errorService.Errors.Count);
            Assert.AreEqual(ESharpErrorCode.ES5002.ToString(), errorService.Errors[0].ErrorCode);
            Assert.AreEqual(EntityBuildErrorType.Error, errorService.Errors[0].Type);
        }

        [TestMethod]
        public void SimpleTestFail3()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Rules.Fail3.es", Resources.Rules_Fail3 ));

            Assert.AreEqual(1, errorService.Errors.Count);
            Assert.AreEqual(ESharpErrorCode.ES5001.ToString(), errorService.Errors[0].ErrorCode);
            Assert.AreEqual(EntityBuildErrorType.Error, errorService.Errors[0].Type);
        }

        [TestMethod]
        public void SimpleTestFail4()
        {
            ESharpProvider provider = new ESharpProvider();
            MockBuildErrorService errorService = new MockBuildErrorService();
            MockTypeFinder typeFinderService = new MockTypeFinder();
            provider.SetService(typeof(IBuildErrorService), errorService);

            EntityRoot root = provider.GetParser().ParseFiles(
                new EntityFile ("Rules.Fail4.es", Resources.Rules_Fail4 ));

            Assert.AreEqual(1, errorService.Errors.Count);
            Assert.AreEqual(ESharpErrorCode.ES5002.ToString(), errorService.Errors[0].ErrorCode);
            Assert.AreEqual(EntityBuildErrorType.Error, errorService.Errors[0].Type);
        }
    }
}
